<template lang="html">
  <div v-loading="loading" class="main-panel">
    <el-button type="primary" @click="xxxxxDialog('post')">添加</el-button>
    <el-dialog v-loading="loading" title="编辑" :visible.sync="xxxxxDiaIsShow" class="diaForm" :append-to-body="false">
      <el-form
        ref="xxxxxRef"
        :model="xxxxxForm"
        :rules="xxxxxRules"
        label-width="140px"
      >
        <el-form-item label="车牌号" prop="plate_number">
          <el-col :span="15">
            <el-input
              v-model="xxxxxForm.plate_number"
              type="text"
              placeholder="请输入车牌号"
            />
          </el-col>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="editXXXXX()"
          >确认</el-button>
          <el-button @click="xxxxxDiaIsShow = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <el-table
      :data="xxxxxTableData"
      border
      style="width: 100%"
    >
      <el-table-column
        prop="create_time"
        label="创建时间"
      />
      <el-table-column
        prop="update_time"
        label="更新时间"
      />
      <el-table-column
        fixed="right"
        label="操作"
        width="100"
        align="center"
      >
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="xxxxxDialog('put',scope.row)">编辑</el-button>
          <el-button type="text" size="small" @click="deleteXXXXX(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="text-align:center;margin-top:20px">
      <el-pagination
        :current-page="xxxxxTableDataPaginateCurrentPage"
        :page-sizes="xxxxxTableDataPaginateTotalPageSizes"
        :page-size="xxxxxTableDataPaginateTotalPageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="xxxxxTableDataPaginateTotal"
        @size-change="xxxxxTableDataHandleSizeChange"
        @current-change="xxxxxTableDataHandleCurrentChange"
      >
        <!-- <span>当前{{ xxxxxTableDataPaginateCurrentPage }}页，</span>
                        <span>共{{ xxxxxTableDataPaginateLastPage }}页</span> -->
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loading: false,
      xxxxxSearchForm: {},
      xxxxxTableData: [],
      xxxxxTableDataPaginateCurrentPage: 1,
      xxxxxTableDataPaginateTotal: 1,
      xxxxxTableDataPaginateLastPage: 1,
      xxxxxTableDataPaginateTotalPageSize: 10,
      xxxxxTableDataPaginateTotalPageSizes: [10, 20, 50, 100],
      xxxxxTab: [],
      xxxxxEditType: 'post',
      xxxxxDiaIsShow: false,
      xxxxxForm: {},
      xxxxxRules: {
        mobile_number: [{
          required: true,
          trigger: 'blur'
        }],
        password: [{
          required: true,
          trigger: 'blur'
        }]
      }
    }
  },
  created() {
    this.getXXXXXList()
  },
  methods: {
    xxxxxTableDataHandleSizeChange(val) {
      // 改变每页显示的条数
      this.xxxxxTableDataPaginateTotalPageSize = val
      this.getXXXXXList(1, this.xxxxxTableDataPaginateTotalPageSize)
    },
    xxxxxTableDataHandleCurrentChange(val) {
      this.xxxxxTableDataPaginateCurrentPage = val
      this.getXXXXXList(val, this.xxxxxTableDataPaginateTotalPageSize)
    },
    getXXXXXList(page = 1, limit = 10) {
      this.loading = true
      this.xxxxxSearchForm.page = page
      this.xxxxxSearchForm.limit = limit
      this.$store.dispatch('main/getXXXXXList',this.xxxxxSearchForm).then((res) => {
        this.xxxxxTableData = res.data.data
        this.xxxxxTableDataPaginateTotal = res.data.total
        this.xxxxxTableDataPaginateCurrentPage = res.data.current_page
        this.xxxxxTableDataPaginateLastPage = res.data.last_page
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    deleteXXXXX(row) {
      // console.log(row)
      this.$confirm('确认删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.loading = true
          this.$store.dispatch('main/deleteXXXXX', { id: row.id }).then((res) => {
            this.getXXXXXList()
            this.loading = false
            this.xxxxxDiaIsShow = false
            this.$message({
              type: 'success',
              message: res.msg
            })
          }).catch(() => {
            // this.loading = false
            return false
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    editXXXXX() {
      switch (this.xxxxxEditType) {
        case 'post':
          this.$refs['xxxxxRef'].validate((valid) => {
            if (valid) {
              this.loading = true
              this.$store.dispatch('xxxxx/postXXXXX', this.xxxxxForm).then((res) => {
                this.getXXXXXList()
                this.xxxxxForm = {}
                this.loading = false
                this.xxxxxDiaIsShow = false
                this.$message({
                  type: 'success',
                  message: res.msg
                })
              }).catch(() => {
                this.loading = false
              })
            } else {
              return false
            }
          })
          break
        case 'put':
          this.$refs['xxxxxRef'].validate((valid) => {
            if (valid) {
              this.loading = true
              this.$store.dispatch('xxxxx/putXXXXX', this.xxxxxForm).then((res) => {
                this.getXXXXXList()
                this.xxxxxForm = {}
                this.loading = false
                this.xxxxxDiaIsShow = false
                this.$message({
                  type: 'success',
                  message: res.msg
                })
              }).catch(() => {
                this.loading = false
              })
            } else {
              return false
            }
          })
          break
      }
    },
    xxxxxDialog(type, row) {
      this.xxxxxDiaIsShow = true
      switch (type) {
        case 'post':
          this.xxxxxEditType = 'post'
          break
        case 'put':
          this.xxxxxEditType = 'put'
          // 这里一定要使用nextTick的方式赋值，不然会卡住input
          this.$nextTick(() => {
            //   this.$set(this.xxxxxForm, 'id', row.id)
          })
          break
        default:
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.main-panel {
  padding: 18px;
  font-size: 10px;
}
</style>
